package com.jf.docsify.common.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.jf.docsify.config.DocsifyConfig;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.druid.DruidPlugin;

/**
 * Description:
 *
 * @author chenwr
 * @version 1.0
 *
 * <pre>
 * 修改记录:
 * 修改后版本           修改人       修改日期         修改内容
 * 2022/7/29.1       chenwr    2022/7/29           Create
 * </pre>
 * @date 2022/7/29
 */
public class JdbcUtil {
    private JdbcUtil() {
    }

    private static DataSource dataSource = null;

    public static Connection getConnection() {
        if (dataSource == null) {
            DruidPlugin druidPlugin = DocsifyConfig.createDruidPlugin();
            druidPlugin.start();
            dataSource = druidPlugin.getDataSource();
        }
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T extends Model> List<T> executeQuery(String sql, Class<T> _class , Object... params) {//查询数据库，需要返回
        Connection conn = getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        List<T> list = new ArrayList<T>();
        try {
            ps = conn.prepareStatement(sql);
            if (null != params) for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            rs = ps.executeQuery();//获得结果集
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            T returnObj = (T) _class.newInstance();
            while (rs.next()) {
                for (int i = 1; i <= columnCount; i++) {
                    String fieldName = metaData.getColumnName(i);
                    Object obj = rs.getObject(fieldName);//通过名字获得结果集中的数据
                    returnObj.set(fieldName, obj);
                }
                list.add(returnObj);
            }
            if (list.size() == 0) return null;
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(rs);
            close(ps);
            close(conn);
        }
        return null;
    }

    //关闭链接操作
    public static void close(Connection conn) {
        if (null != conn) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //关闭Statement-->发送sql语句
    public static void close(Statement stat) {
        if (null != stat) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //关闭ResultSet
    public static void close(ResultSet rs) {
        if (null != rs) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}